class Mockolo < Formula
  desc "Efficient Mock Generator for Swift"
  homepage "https://github.com/uber/mockolo"
  url "https://github.com/uber/mockolo/archive/refs/tags/2.3.1.tar.gz"
  sha256 "b4cf043d0f606b8e7744a5febdcef907c63f38fd35c4b11cb14ce90242eb56e5"
  license "Apache-2.0"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "21db991c7dc13e12fbd75ed7f6692222ed005c211eef03f27a18a23b115cf06f"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "1b0726cdef7ae2ee5e4248a36dce0c4bdec6a6c8fcb1f9a7cd65f3e206819910"
    sha256 cellar: :any_skip_relocation, sonoma:        "6e8778005fb76e85b0eba303110ef437760803943622425ba7bf08f261018bda"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "de62f57ed9cb0ede299a2b323a257edbeca7336c243433f4b9eda7ea48926b24"
  end

  depends_on xcode: ["15.3", :build]

  uses_from_macos "swift" => :build

  def install
    args = if OS.mac?
      ["--disable-sandbox"]
    else
      ["--static-swift-stdlib"]
    end
    system "swift", "build", *args, "-c", "release", "--product", "mockolo"
    bin.install ".build/release/mockolo"
  end

  test do
    (testpath/"testfile.swift").write <<~SWIFT
      /// @mockable
      public protocol Foo {
          var num: Int { get set }
          func bar(arg: Float) -> String
      }
    SWIFT
    system bin/"mockolo", "-srcs", testpath/"testfile.swift", "-d", testpath/"GeneratedMocks.swift"
    assert_path_exists testpath/"GeneratedMocks.swift"
    output = <<~SWIFT.gsub(/\s+/, "").strip
      ///
      /// @Generated by Mockolo
      ///
      public class FooMock: Foo {
        public init() { }
        public init(num: Int = 0) {
            self.num = num
        }

        public private(set) var numSetCallCount = 0
        public var num: Int = 0 { didSet { numSetCallCount += 1 } }

        public private(set) var barCallCount = 0
        public var barHandler: ((Float) -> String)?
        public func bar(arg: Float) -> String {
            barCallCount += 1
            if let barHandler = barHandler {
                return barHandler(arg)
            }
            return ""
        }
      }
    SWIFT
    assert_equal output, shell_output("cat #{testpath/"GeneratedMocks.swift"}").gsub(/\s+/, "").strip
  end
end
